<?php
namespace app\common\taglib;

use think\template\TagLib;

class Dl extends TagLib
{
    /**
     * 定义标签列表
     */
    protected $tags   =  [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        //列表
        'list'      => ['attr' => 'db,field,id,limit,order,lang', 'close' => 1],
        /**
         * {dl:model db="carousel" like="type" value="$navid" model="$browsertype"}幻灯片
         * {Dl:model db="online" field="QQ,name" lang="$lang" id="vo" limit="5"} 在线客服
         */
        'model'      => ['attr' => 'db,field,id,limit,order,where,lang,like,value', 'close' => 1],
        //留言列表
        'mlist'      => ['attr' => 'db,field,id,limit,status,order,lang', 'close' => 1],
    ];

    /**
     * 单数据库
     */
    public function tagmodel($tag, $content)
    {
        $id     = empty($tag['id']) ?  'vo' : $tag['id'];
        $limit  = empty($tag['limit']) ? '0, 5' : $tag['limit'];
        $field  = empty($tag['field']) ? '*' : $tag['field'];
        $order  = empty($tag['order']) ? 'id DESC' : $tag['order'];

        $where = 'status = \'1\'';
        if (!empty($tag['lang'])) {
            $where .= ' and lang = \'' . $tag['lang'] . '\'';
        }

        if (!empty($tag['like'])&&isset($tag['value'])) {
            $where .= ' and '.$tag['like'].' like \'%,' . $tag['value'] . ',%\'';
        } 
        
        if (!empty($tag['where'])&&isset($tag['wherevalue'])) {
            $where .= ' and '.$tag['where'].' = \'' .  $tag['wherevalue']. '\'';
        }

        if (!empty($tag['model'])) {
            $where .= ' and model = \'' . $tag['model'] . '\'';
        }

        $parse = '<?php ';
        $parse .= '$link_arr=model("' . $tag['db'] . '")
                            ->field("' . $field . '")
                            ->where("' . $where . '")
                            ->limit("' . $limit . '")
                            ->order("' . $order . '")
                            ->select();';
        $parse .= ' ?>';
        $parse .= '{volist name="link_arr" id="' . $id . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }


    /**
     * 列表
     */
    public function taglist($tag, $content)
    {
        $id     = empty($tag['id']) ?  'vo' : $tag['id'];
        $order  = empty($tag['order']) ? 'a.id DESC' : $tag['order'];
        $field  = empty($tag['field']) ? '*' : $tag['field'];
        $limit  = empty($tag['limit']) ? '0, 5' : $tag['limit'];
        $where = 'a.status = \'1\'';
        if (!empty($tag['cid'])) {
            $where .= ' and a.cid = ".' . $tag['cid'] . '."';
        }
        if (!empty($tag['lang'])) {
            $where .= ' and a.lang = \'".' . $tag['lang'] . '."\'';
        }

        $parse = '<?php ';
        $parse .= '$list_arr=model("' . $tag['db'] . '")
                             ->alias("a")
                             ->join("__NAV__ c", "c.id= a.cid AND c.lang= a.lang AND c.status = a.status")
                             ->field("' . $field . '")
                             ->where("' . $where . '")
                             ->order("' . $order . '")
                             ->limit("' . $limit . '")
                             ->select();';
        $parse .= ' ?>';
        $parse .= '{volist name="$list_arr" id="' . $id . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    /**
     * 留言列表
     */
    public function tagmlist($tag, $content)
    {
        $id     = empty($tag['id']) ?  'vo' : $tag['id'];
        $limit  = empty($tag['limit']) ? '0, 5' : $tag['limit'];
        $field  = empty($tag['field']) ? '*' : $tag['field'];
        $order  = empty($tag['order']) ? 'a.id DESC' : $tag['order'];

        $where = '1=1';
        if (!empty($tag['aid'])) {
            $where .= ' and a.aid = ".' . $tag['aid'] . '."';
        }
        if (!empty($tag['lang'])) {
            $where .= ' and a.lang = \'".' . $tag['lang'] . '."\'';
        }
        if (!empty($tag['status'])) {
            $where .= ' and a.status = \'' . $tag['status'] . '\'';
        }

        $parse = '<?php ';
        $parse .= '$list_arr=model("' . $tag['db'] . '")
                             ->alias("a")
                             ->join("__USER__ u", "u.id= a.uid AND u.status = a.status","LEFT")
                             ->field("' . $field . '")
                             ->where("' . $where . '")
                             ->order("' . $order . '")
                             ->limit("' . $limit . '")
                             ->select();';
        $parse .= ' ?>';
        $parse .= '{volist name="$list_arr" id="' . $id . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
}
